package my.ts.code;

import java.util.ArrayList;

import my.ts.code.adapter.ServiceAdapter;
import my.ts.code.net.AppContext;
import my.ts.code.objects.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class HomepageScreen extends BaseActivity {

	private GridView gridview;
	ServiceAdapter adapter;
	ArrayList<Service> lstService = new ArrayList<Service>();
	Context context;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		context = this;

		setContentView(R.layout.homepage_screen);
		gridview = (GridView) findViewById(R.id.grid);

		init();

	}

	private void init() {

		Service s1 = new Service("Tin tức", getResources().getDrawable(
				R.drawable.news_icon), Config.TIN_TUC);
		lstService.add(s1);

		Service s2 = new Service("Truyện", getResources().getDrawable(
				R.drawable.stories_icon), Config.TRUYEN);
		lstService.add(s2);

		Service s3 = new Service("Game", getResources().getDrawable(
				R.drawable.games_icon), Config.GAME);
		lstService.add(s3);

		Service s4 = new Service("Hình ảnh", getResources().getDrawable(
				R.drawable.photos_icon), Config.HINH_ANH);
		lstService.add(s4);

		Service s7 = new Service("Clips", getResources().getDrawable(
				R.drawable.clips_icon), Config.VIDEO);
		lstService.add(s7);

		Service s8 = new Service("Nhạc", getResources().getDrawable(
				R.drawable.music_icon), Config.MUSIC);
		lstService.add(s8);

		Service s9 = new Service("Audio", getResources().getDrawable(
				R.drawable.audio_icon), Config.AUDIO);
		lstService.add(s9);

		Service s10 = new Service("Thị trường", getResources().getDrawable(
				R.drawable.bussiness_icon), Config.CURRENCY);
		lstService.add(s10);

		Service s11 = new Service("Thiệp nhạc", getResources().getDrawable(
				R.drawable.music_card_icon), Config.POSTCARD);
		lstService.add(s11);

		Service s12 = new Service("Xổ số", getResources().getDrawable(
				R.drawable.lottery_icon), Config.LOTTERY);
		lstService.add(s12);

		adapter = new ServiceAdapter(getBaseContext(), 0, lstService);
		gridview.setAdapter(adapter);
		adapter.notifyDataSetChanged();

		gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parents, View arg1,
					int postion, long id) {

				Intent intent = new Intent(HomepageScreen.this,
						CategoryScreen.class);
				Bundle b = new Bundle();
				b.putInt("id", lstService.get(postion).getId());
				b.putString("title", lstService.get(postion).getTitle());
				startActivity(intent.putExtras(b));

			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_BACK:
				System.out.println("ON KEY BACK");
				showDialogConfirmExit(context, HomepageScreen.this,
						"Thông báo", "Bạn có muốn thoát khỏi ứng dụng?",
						true);
				return true;
			}
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		AppContext.instance.close();
	}
}
